package cn.jdy.plugins.admin;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import cn.jdy.authority.entity.User;
import cn.jdy.common.ctrl.BaseCtrl;
import cn.jdy.framework.log.BaseLog;
import cn.jdy.platform.service.ConfService;
import cn.jdy.plugins.entity.FastMail;
import cn.jdy.plugins.service.FastMailService;


/**
 * @title 快递网点控制类
 * @date 2015-11-19
 * @author daihu
 */
@Controller
@RequestMapping("/fastmail")
public class FastMailCtrl extends BaseCtrl {

	@Autowired
	private FastMailService fastmailService;
	@Autowired
	private ConfService confService;
	//默认列表路径
	private static String viewName = "redirect:/fastmail/list?s=";

	/**
	 * 初始化
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView list(FastMail entity, @RequestParam(required = false) Character s) {
		ModelAndView mv = new ModelAndView("/postoffice/fastMailList.jsp");
		if(s != null)
			switch(s) {
				case '1': mv.addObject("msg", "保存成功！");break;
				case '2': mv.addObject("msg", "更新成功！");break;
				case '3': mv.addObject("msg", "删除成功！");break;
				case '4': mv.addObject("msg", "操作失败！");break;
				default: break;
			}
		mv.addObject("entity", entity);
		return mv;
	}

	/**
	 * 异步查询
	 * @param fastmail
	 * @return
	 */
	@RequestMapping("/queryByPage")
	@ResponseBody
	public Map<String, Object> queryByPage(FastMail fastmail) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {
			jsonMap.put("dataList", fastmailService.query(fastmail));
			jsonMap.put("iTotalDisplayRecords", fastmailService.count(fastmail));
			jsonMap.put("iTotalRecords", fastmailService.count());
		} catch(Exception e) {
			BaseLog.e(this.getClass(), "查询快递网点异常", e);
		}
		return jsonMap;
	}

	/**
	 * 新增
	 * @return
	 */
	@RequestMapping("/edit")
	public ModelAndView edit() {
		return edit(null);
	}

	/**
	 * 编辑
	 * @param id
	 * @return
	 */
	@RequestMapping("/edit/{id}")
	public ModelAndView edit(@PathVariable Long id) {
		ModelAndView mv = new ModelAndView("/postoffice/fastMailDetail.jsp");
		mv.addObject("data", id==null||id==0 ? id : this.fastmailService.find(id));
		return mv;
	}

	/**
	 * 保存（更新）快递网点数据
	 * @param fastmail
	 * @return
	 */
	@RequestMapping("/save")
	public ModelAndView save(FastMail fastmail) {
		ModelAndView mv = new ModelAndView();
		try {
			if(fastmail.getId()==null) {// 新增快递网点
				User user = getUser();
				fastmail.setUserid(user.getId());
				fastmail.setUsername(user.getUsername());
				this.fastmailService.save(fastmail);
				mv.setViewName(this.viewName.concat("1"));
			} else {//更新快递网点数据
				this.fastmailService.update(fastmail);
				mv.setViewName(this.viewName.concat("2"));
			}
		} catch(Exception e) {
			mv.setViewName(this.viewName.concat("4"));
			BaseLog.e(getClass(), "快递网点保存异常！", e);
		}

		return mv;
	}
	
	/**
	 * 删除 快递网点数据
	 * @return
	 */
	@RequestMapping("/del/{id}")
	public ModelAndView del(@PathVariable Long id) {
		ModelAndView mv = new ModelAndView();
		try {
			this.fastmailService.delete(id);
			mv.setViewName(this.viewName.concat("3"));
		} catch(Exception e) {
			mv.setViewName(this.viewName.concat("4"));
			BaseLog.e(getClass(), "删除快递网点异常！", e);
		}
		return mv;
	}

}
